<template>
    <Form ref="formItem" :model="formItem" :label-width="120" :rules="ruleValidate">
        <FormItem label="班级" >
            <Select v-model="formItem.eclass_id" @on-change="students">
                <Option v-for="(item,ind) in EclassInfoList" :value="item.eclass_id" :key="ind">{{item.eclass_name.zh?item.eclass_name.zh:item.eclass_name.mn}}</Option>
            </Select>
        </FormItem>
        <FormItem label="学生" prop="student_id" v-if="infoId">
            <Select v-model="formItem.student_id">
                <Option v-for="(item,index) in StuBasicInfoList" :value="item.user_id" :key="index">{{ item.student_name+item.identityard_num }}</Option>
            </Select>
        </FormItem>
        <FormItem label="学生" prop="student_id" v-else>
            <Select v-model="formItem.student_id" multiple>
                <Option v-for="(item,index) in StuBasicInfoList" :value="item.user_id" :key="index">{{ item.student_name+item.identityard_num }}</Option>
            </Select>
        </FormItem>
        <FormItem label="请假开始日期" prop="start_time">
            <DatePicker type="datetime" format="yyyy-MM-dd HH:mm" v-model="formItem.start_time"  placeholder="请假开始日期" style="width:100%"></DatePicker>
        </FormItem>
        <FormItem label="请假结束日期" prop="end_time">
            <DatePicker type="datetime" format="yyyy-MM-dd HH:mm" v-model="formItem.end_time"  placeholder="请假结束日期" style="width:100%"></DatePicker>
        </FormItem>
        <FormItem label="请假种类" >
            <Input v-model="chaxunArr.name" disabled type="text" ></Input>                                                           
        </FormItem>
        <FormItem label="请假分类">
            <Select v-model="formItem.leave_classification" >
                <Option v-for="item in DictionaryList" :value="item.dictionary_id" :key="item.dictionary_id">{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
            </Select>
        </FormItem>
        <FormItem label="班主任是否担保" prop="is_guarantee">
            <Select v-model="formItem.is_guarantee">
                <Option v-for="item in guaranteeList" :value="item.value" :key="item.value">{{item.label}}</Option>
            </Select>
        </FormItem>
        <FormItem label="请假原因">
            <Input v-model="formItem.dormitory_outside_cause.zh" type="textarea" :autosize="{minRows: 2,maxRows: 4}" ></Input>                                       
        </FormItem>
        <FormItem label="班主任意见">
            <Input v-model="formItem.head_teacher_opinion.zh" type="textarea" :autosize="{minRows: 2,maxRows: 4}" ></Input>                              
        </FormItem>
            <FormItem label="备注">
            <Input v-model="formItem.remark.zh" type="textarea" :autosize="{minRows: 2,maxRows: 4}" ></Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="submit('formItem')" :loading="btnload">{{infoId?'修改':'申请'}}</Button>
        </FormItem>
    </Form>
</template>

<script>
export default {
    props:['infoId','chaxunArr'],
    data () {
        return {
            btnload:false,
            EclassInfoList:[],
            StuBasicInfoList:[], 
            DictionaryList:[], 
            formItem: {    
                student_id:this.infoId!=null?null:[],
                start_time:this.$util.dateTime(),
                end_time:'',  
                leave_classification:null,
                student_leave_kind:this.chaxunArr.id,  
                dormitory_outside_cause:{ 
                    zh:'',mn:'',en:'',
                },
                head_teacher_opinion:{  
                    zh:'',mn:'',en:'',
                },
                remark:{  
                    zh:'',mn:'',en:'',
                },
                is_guarantee:0,
                eclass_id:null,
            },
            guaranteeList:[
                {
                    label:'不担保',
                    value:0,
                },
                {
                    label:'担保',
                    value:1,
                },
            ],
            addUrl:'api/leave/v1/student_leave/dormitory_leave_apply',
            editUrl:'api/leave/v1/student_leave/getone/',
            updateUrl:'api/leave/v1/student_leave/dormitory_leave_edit/',
            ruleValidate: {
                leave_classification: [
                    { required: true, type: 'number', message: '请选择请假分类', trigger: 'change' }
                ],
                is_guarantee: [
                    { required: true, type: 'number', message: '请选择班主任是否担保', trigger: 'change' }
                ],
                start_time: [
                    { required: true, type: 'date', message: '请选择请假开始日期', trigger: 'change' }
                ],
                end_time: [
                    { required: true, type: 'date', message: '请选择请假结束日期', trigger: 'change' }
                ],
            }
        }
    },
    methods: {
        /************************************************************
         * 获取数据字典里的请假分类
         ************************************************************/
        dictionary(){
            this.DictionaryList =[]
            this.$http.get('api/dictionary/v1/dictionary/pidquerys?leave_classification').then(val=>{ 
                this.DictionaryList=val.data.leave_classification               
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
            })
        },
        /************************************************************
         * 获取班级
         ************************************************************/
        eclassInfo(){
            this.$http.get('api/safety/v1/certifications/classlist').then((response) => {
                this.EclassInfoList=response.data          
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
            })
        },
        /************************************************************
         * 获取学生
         ************************************************************/
        students(val){
            this.StuBasicInfoList = []
            if(val!=null){
                this.$http.get(`api/leave/v1/student_leave/get_student/${val}`).then((response) => {                   
                    response.data.forEach((item,index) => {
                        this.StuBasicInfoList.push({
                            user_id:item.user_id,
                            student_name:item.studentbase.student_name.zh,
                            identityard_num:item.studentbase.identityard_num,
                        })
                    });        
                }).catch(err => {
                    if (err.code && err.code == 422) {
                        this.$Message.error(err.message);
                    }
                })
            }
        },
        /************************************************************
         * 提交
         ************************************************************/
        submit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.btnload = true
                    if (this.infoId) {
                        if(this.formItem.student_id!=null){
                            this.editSubmit ()
                        }else{
                            this.$Message.error('请选择学生');
                            this.btnload = false 
                        }
                    } else {
                        if(this.formItem.student_id.length!=0){
                            this.addSubmit ()
                        }else{
                            this.$Message.error('请选择学生');
                            this.btnload = false 
                        }
                    }
                } else {
                    this.$Message.error('验证失败');
                    this.btnload = false 
                }
            })
        },
        
        /************************************************************
         * 执行添加数据
         ************************************************************/
        addSubmit () { 
            this.$http.post(this.addUrl,this.formItem).then(val=>{
                this.$Message.success(val.message);
                this.$emit('requery',true)                                                      
                this.btnload = false
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.btnload = false
            })
        },
        
        /************************************************************
         * 执行修改数据
         ************************************************************/
        editSubmit () {
            this.$http.put(this.updateUrl+this.infoId,this.formItem).then(val=>{
                this.$Message.success(val.message);
                this.$emit('requery',true)   
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.btnload = false 
            })
        },
        
        /************************************************************
         * 单条数据查询（修改）
         ************************************************************/
        infodata(){
            this.$http.get(this.editUrl + this.infoId).then(val=>{ 
            let infoOld = JSON.parse(JSON.stringify(val.data)) 
                this.formItem.eclass_id = parseInt(infoOld.eclass_id)  
                this.formItem.student_id = parseInt(infoOld.student_id)
                this.formItem.start_time = infoOld.start_time.toString()
                this.formItem.end_time = infoOld.end_time
                this.formItem.dormitory_outside_cause = infoOld.dormitory_outside_cause
                this.formItem.head_teacher_opinion = infoOld.head_teacher_opinion
                this.formItem.remark= infoOld.remark
                if(infoOld.leave!=null){
                    this.formItem.leave_classification = parseInt(infoOld.leave.leave_classifications.dictionary_id)
                }
                this.formItem.is_guarantee = infoOld.is_guarantee   
                
                this.students(this.formItem.eclass_id)
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
            })
        },
    },
    created () {
        this.dictionary();
        this.eclassInfo();
        if (this.infoId){            
            this.infodata ()
        }   
    },
}
</script>